<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增入库管理')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
<div class="main-content">
    <form id="form-fEnter-add" class="form-horizontal">
        <input name="thingId" type="hidden" id="treeId"/>
        <input name="thingTypeId" type="hidden" id="thingTypeId"/>
        <input name="averageDanjia" type="hidden" id="averageDanjia"/>
        <input name="mohu" type="hidden" id="mohu"/>

        <h4 class="form-header h4">基本信息</h4>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">物品目录：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <input readonly="readonly" required name="thingName" onclick="selectThingTree()" id="treeName" type="text" placeholder="请选择物品" class="form-control">
                            <span class="input-group-addon"><i class="fa fa-search"></i></span>
                        </div>
                        <span style="color: red" id="tishi"></span>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">收支项目：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <input readonly="readonly" required name="thingType" onclick="selectThingTypeTree()" id="thingType" type="text" placeholder="请选择收支项目" class="form-control">
                            <span class="input-group-addon"><i class="fa fa-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">物品名称：</label>
                    <div class="col-sm-8">
                        <input id="createName" name="createName" placeholder="" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">发票号：</label>
                    <div class="col-sm-8">
                        <input name="lnvoiceNumber" placeholder="" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">数量（发票信息）：</label>
                    <div class="col-sm-8">
                        <input id="fshuliang" oninput="jisuan()" name="fshuliang" placeholder="" class="form-control" type="number" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">记账凭证：</label>
                    <div class="col-sm-8">
                        <input name="jizhang" placeholder="" class="form-control" type="text" >
                        <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 财管中的单据号</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">发票金额+税额：</label>
                    <div class="col-sm-8">
                        <input id="zonge" oninput="jisuan()" name="zonge" placeholder="" class="form-control" type="number" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label ">入库时间：</label>
                    <div class="col-sm-8">
                        <input name="enterTime" type="text" class="form-control" id="enterTime" placeholder="yyyy-MM-dd" >
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">含税单价（系统计算项）：</label>
                    <div class="col-sm-8">
                        <input readonly="readonly" id="fdanjia" name="fdanjia" placeholder="" class="form-control" type="number" required>
                        <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 发票金额+税额 除 数量（发票信息）</span>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">品牌：</label>
                    <div class="col-sm-8">
                        <input id="pinpai" name="pinpai" placeholder="" class="form-control" type="text" >
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">物品单位（发票信息）：</label>
                    <div class="col-sm-8">
                        <input name="danwei" placeholder="" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">型号：</label>
                    <div class="col-sm-8">
                        <input id="xinghao" name="xinghao" placeholder="" class="form-control" type="text" >
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">最小度量单位：</label>
                    <div class="col-sm-8">
                        <select id="jiliangdanwei" name="jiliangdanwei" class="form-control m-b" th:with="type=${@dict.getType('f_jiliangdanwei')}">
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">最小度量物品数量：</label>
                    <div class="col-sm-8">
                        <input id="guige" required oninput="jisuan()" name="guige" placeholder="" class="form-control" type="number" >
                        <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 购买物品发票单位中最小物品的单位数量</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">最小度量物品数量（系统计算项）：</label>
                    <div class="col-sm-8">
                        <input readonly="readonly" id="shuliang" name="shuliang" class="form-control" type="number" required>
                        <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 数量（发票信息）乘 最小度量物品数量</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">最小度量含税单价：</label>
                    <div class="col-sm-8">
                        <input id="danjia"  name="danjia" class="form-control" type="number" required>
                        <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 发票金额+税额 除 最小度量物品数量（系统计算项）</span>
                    </div>
                </div>
            </div>
        </div>

        <h4 class="form-header h4">其他信息</h4>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-sm-2 control-label">图片：</label>
                    <div class="col-sm-10">
                        <input type="hidden" name="imgList">
                        <div class="file-loading">
                            <input class="form-control file-upload" id="imgList" name="file" type="file">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-xs-2 control-label">备注：</label>
                    <div class="col-xs-10">
                        <textarea name="remark" maxlength="500" class="form-control" rows="3"></textarea>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>

<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存
        </button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭
        </button>
    </div>
</div>


<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<th:block th:include="include :: select2-js" />
<script th:inline="javascript">
    var prefix = ctx + "financial/fEnter"

    var currentDate = new Date();
    var year = currentDate.getFullYear();
    var month = currentDate.getMonth() + 1;
    var day = currentDate.getDate();

    layui.use('laydate', function () {
        var laydate = layui.laydate;
        laydate.render({
            elem: '#enterTime',
            max:year+"-"+month+"-"+day
        });
    });

    $("#form-fEnter-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        var thingType = $("#thingType").val();
        var shuliang = $("#shuliang").val();
        if(thingType=="其他"){
            var zonge = $("#zonge").val()
            if(!zonge){
                $.modal.msgWarning("请填写 发票金额+税额");
                return
            }
            if(!shuliang){
                $("#shuliang").val("1")
            }
            $("#treeId").val("0")

            var str = $("#pinpai").val()+"牌"+$("#xinghao").val()+$("#createName").val()+$("#guige").val()
            $("#mohu").val(str)
            $.operate.saveTab(prefix + "/add", $('#form-fEnter-add').serialize());
        }else{
            if ($.validate.form()) {
                var str = $("#pinpai").val()+"牌"+$("#xinghao").val()+$("#createName").val()+$("#guige").val()
                $("#mohu").val(str)
                $.operate.saveTab(prefix + "/add", $('#form-fEnter-add').serialize());
            }
        }
    }

    $(".file-upload").fileinput({
        uploadUrl: ctx + 'common/upload',
        maxFileCount: 1,
        autoReplace: true
    }).on('fileuploaded', function (event, data, previewId, index) {
        $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
    }).on('fileremoved', function (event, id, index) {
        $("input[name='" + event.currentTarget.id + "']").val('')
    })

    function selectThingTree() {
        var treeId = $("#treeId").val();
        var deptId = $.common.isEmpty(treeId) ? "0" : $("#treeId").val();
        var url = ctx + "financial/fThing/selectFThingTree/" + deptId+"/1111";
        var options = {
            title: '选择物品',
            width: "380",
            url: url,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }
    function doSubmit(index, layero) {
        var body = $.modal.getChildFrame(index);
        $("#treeId").val(body.find('#treeId').val());
        $("#treeName").val(body.find('#treeName').val());

        var jiliangdanwei = body.find('#thingUnit').val()
        if(jiliangdanwei){
            $('#jiliangdanwei option').each(function() {
                var value = $(this).val(); // 获取当前选项的值
                var text = $(this).text(); // 获取当前选项的文本
                if(jiliangdanwei == text){
                    $("#jiliangdanwei").val(value);
                    $("#select2-jiliangdanwei-container").html(text);
                }
            });
        }
        $.modal.close(index);
    }

    function selectThingTypeTree() {
        var url = ctx + "financial/thingType/selectThingTypeTree/0/1111";
        var options = {
            title: '选择收支项目',
            width: "380",
            url: url,
            callBack: doSubmit2
        };
        $.modal.openOptions(options);
    }
    function doSubmit2(index, layero) {
        var body = $.modal.getChildFrame(index);
        $("#thingTypeId").val(body.find('#treeId').val());
        $("#thingType").val(body.find('#treeName').val());
        $.modal.close(index);
    }

    function jisuan() {
        var zonge = $("#zonge").val();
        var fshuliang = $("#fshuliang").val();
        if(zonge && fshuliang){
            var danjia = zonge/fshuliang
            var weishu = (danjia+"").split(".")[1]
            if(weishu && weishu.length>2){
                danjia = danjia.toFixed(2)
            }
            $("#fdanjia").val(danjia)
        }

        var guige = $("#guige").val();
        if(guige && fshuliang){
            var shuliang = guige*fshuliang
            $("#shuliang").val(shuliang)
            if(zonge){
                var danjia = zonge/shuliang
                var weishu = (danjia+"").split(".")[1]
                if(weishu && weishu.length>2){
                    danjia = danjia.toFixed(2)
                }
                $("#danjia").val(danjia)
            }
        }

        // var zonge = 0;
        // var fjage = $("#fjiage").val();
        // var shuie = $("#shuie").val();
        // if(fjage){
        //     zonge = zonge + parseFloat(fjage)
        // }
        // if(shuie){
        //     zonge = zonge + parseFloat(shuie)
        // }
        // if(zonge != 0){
        //     $("#zonge").val(zonge)
        // }
        //
        // var shuliang = 0;
        // var guige = $("#guige").val()
        // var fshuliang = $("#fshuliang").val()
        // if(fshuliang){
        //     shuliang = fshuliang
        // }
        // if(guige){
        //     guige = guige.replace(/\D/g, '')
        // }
        // if(guige && fshuliang){
        //     shuliang = parseInt(guige) * parseInt(fshuliang)
        // }
        // if(shuliang != 0){
        //     $("#shuliang").val(shuliang)
        // }
        // if(shuliang != 0 && zonge != 0){
        //     var danjia = zonge/shuliang
        //     var weishu = (danjia+"").split(".")[1]
        //     if(weishu && weishu.length>2){
        //         danjia = danjia.toFixed(2)
        //     }
        //     $("#danjia").val(danjia)
        // }
    }
    
    function wupin() {
        $("#averageDanjia").val("")
        $("#tishi").html("")
        $("#danwei").html("")

        $.ajax({
            url: prefix + '/selectAverageDanjia/' + $("#treeId").val() + "/" + $("#thingType").val(),
            type: 'GET',
            success: function (res) {
                if (res.code == 0) {
                    if (res.data.danjia != "") {
                        $("#tishi").html("平均单价" + res.data.danjia)
                        $("#averageDanjia").val(res.data.danjia)
                    }
                    if (res.data.thingUnit != "") {
                        $("#danwei").html("/" + res.data.thingUnit)
                    }
                }
            },
            error: function () {

            }
        });
    }

    function thingTypeChange() {
        if ($("#treeId").val()) {

        }
    }


</script>
</body>
</html>